package cn.szzsi.business.controller.admin;

import cn.szzsi.business.service.BusinessBalanceDetailService;
import cn.szzsi.business.service.BusinessBalanceService;
import cn.szzsi.common.sms.SmsEmayUtil;
import cn.szzsi.common.util.BackUtil;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.common.util.RegexUtils;
import cn.szzsi.model.BusinessBalance;
import cn.szzsi.model.BusinessBalanceDetail;
import cn.szzsi.model.CashBank;
import cn.szzsi.model.Member;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.ehcache.CacheKit;

import java.math.BigDecimal;
import java.util.List;

public class BusinessCashingManageController extends Controller{
    public void index(){
        String shopId=getAttr("shopId");
        Member member=Member.dao.findById(shopId);
        BusinessBalance businessBalance=BusinessBalance.dao.getByBusinessId(shopId);
        setAttr("businessBalance",businessBalance);
        setAttr("member",member);
        render(CommonUtil.TEMPLATE_PATH+"/business/cashingmanage/business_tixian_input.html");
    }

    @Before(Tx.class)
    public void ajaxlist(){
        String shopId=getAttr("shopId");
        List<BusinessBalanceDetail>businessBalanceDetailList=BusinessBalanceDetail.dao.find("select * from sys_business_balance_detail where type=0 and bid=?",shopId);
        renderJson(businessBalanceDetailList);
    }

    /**
     * 提现申请
     */
    public void cashingApply(){
        String cashAmount=getPara("cashAmount");//提现金额
        Integer bankType=getParaToInt("bankType");//银行卡类型
        String phonecode=getPara("phonecode");//验证码
        String shopId=getAttr("shopId");
        Member business=Member.dao.findById(shopId);
        String phone=business.getPhone();
        if(CacheKit.get("sms",phone)==null){
            renderJson(BackUtil.fail(""));
            renderJson(BackUtil.fail("手机验证码已过期,请重新获取!"));
            return;
        }
        if(!phonecode.equals(CacheKit.get("sms",phone).toString())){
            renderJson(BackUtil.fail("手机验证码不正确!"));
            return;
        }
        CashBank cashBank=CashBank.dao.getByBid(business.getId());
        String cashingAccount=null;
        if(0==bankType){
            if(StrKit.isBlank(cashBank.getCardNumber())){
                renderJson(BackUtil.fail("请在账户信息中完善提现账号!"));
                return;
            }
            cashingAccount=cashBank.getCardNumber();
        }
        if(1==bankType){
            if(StrKit.isBlank(cashBank.getAliPayAccount())){
                renderJson(BackUtil.fail("请在账户信息中完善提现账号!"));
                return;
            }
            cashingAccount=cashBank.getAliPayAccount();
        }
        if(2==bankType){
            if(StrKit.isBlank(cashBank.getWeChatAccount())){
                renderJson(BackUtil.fail("请在账户信息中完善提现账号!"));
                return;
            }
            cashingAccount=cashBank.getWeChatAccount();
        }
        BusinessBalance businessBalance=BusinessBalance.dao.getByBusinessId(business.getId());
        BigDecimal balance=businessBalance.getBalance();//账户余额
        if(new BigDecimal(cashAmount).compareTo(balance)==1){//提现金额大于账户余额
            renderJson(BackUtil.fail("提现金额不能大于账户余额哦!"));
            return;
        }

        //增加提现记录
        BusinessBalanceDetailService.service.saveInfo(business.getId(),new BigDecimal(cashAmount).multiply(new BigDecimal(-1)),0,bankType,cashingAccount,1,"发起提现申请!");

        //扣除提现金额,更新余额
        BusinessBalanceService.service.updateBalance(business.getId(),new BigDecimal(cashAmount).multiply(new BigDecimal(-1)));

        //冻结提现金额
        businessBalance.setFreezeMoney(businessBalance.getFreezeMoney().add(new BigDecimal(cashAmount)));
        businessBalance.update();
        renderJson(BackUtil.success("操作成功!"));

    }

    /**
     * 获取手机验证码
     */
    @Clear
    public void validateCaptcha(){
        String phone =getPara("phone");
        if(StrKit.isBlank(phone)){
            renderJson(BackUtil.fail("请输入正确的手机号"));
            return;
        }
        /*if(StrKit.notBlank(phone)&&!RegexUtils.checkMobile(phone)){
            renderJson(BackUtil.fail("请输入正确的手机号"));
            return;
        }*/
        Member member= Member.dao.getByPhone(phone);
        if(member==null){
            renderJson(BackUtil.fail("手机号不存在!"));
            return;
        }
        String captcha = CommonUtil.getSixRandom();//六位纯数字验证码
        //同一个手机号限制一分钟之内只能发送一条
        Long lastTimeMillis = CacheKit.get("sms_last_time",phone);
        if(lastTimeMillis!=null){
            Long currentTimeMillis=System.currentTimeMillis();
            if((currentTimeMillis-lastTimeMillis)<60000){
                renderJson(BackUtil.fail("您的操作过于频繁,请稍后再试!!"));
                return;
            }

        }
        //同一个手机号限制一分钟之内只能发送一条
        //【微米荟商城】您的验证码:686260 ，请勿泄露！感谢您的使用！
        String message = "【微米荟商城】您的验证码:"+captcha+" ，请勿泄露！感谢您的使用！";
        boolean sendResult= SmsEmayUtil.sendToSms("0",phone,message,"yzm","wmh",captcha);
        if(sendResult){
            renderJson(Ret.ok("info","发送成功!"));
        }else{
            renderJson(BackUtil.fail("发送失败!"));
        }
    }
}
